Assignment 5: Spring into action

Due Friday, October 4, before midnight

The goals for this assignment are:

  • Implement simple player controls

  • Implement a spring follow camera that follows the player

  • (Optional) Use transparency to prevent objects from obscuring the camera

1. Getting Started

It is not necessary to fetch from the upstream repository this week. Instead,

  • Create a new scene, called A05_Player, and save it to your HelloUnity/Assets/Scenes folder.

Scene and Player Setup

  • Initialize your scene with the prefab containing your level from assignments 3 and 4

  • Add your character from assignment 4. After completing this assignment, we recommend you create a prefab for your character and camera.

2. Assignment Goals

The primary goal of this assignment is to implement a character that represents your player. We will implement a third-person character with a spring follow camera. You should also implement a feature that makes objects that block the character transparent. Next week, we will add animation to your character. Thus, the features needed for this week are:

  • WASD keys for controlling the movement of the character forward, left, down, and right respectively

  • Mouse movement should control the preferred heading and pitch of the camera. Pressing the spacebar should reset the camera behind the character.

2.1. Player controls

Write a script, Scripts/PlayerControls.cs, that is a component for the root of the player character.

  • The keyboard keys WASD should move the player forward/back/left/right relative to its current position

  • Add two parameters to your script to control the linear speed and turning speed of the character

The algorithm should look something like this in Update()

if the player pressed the W key
   Move forward
else if the player pressed the S key
   Move backward
else if the player presses the A key
   Turn left
else if the player presses the D key
   Turn right

Some questions to think about when you implement this feature:

  • How can we get the forward and up directions of a GameObject in Unity?

  • How can we set parameters in a script so that we can give them values in the Unity IDE?

  • How can we use time to ensure the player moves smoothly?

  • In Unity, how can you specify a rotation around the Y axis?

  • When we translate the player we add a displacement. How do we "add", or concatenate, a rotation to the player?

A05 PlayerControls

2.2. Third person camera

We will complete this feature in two steps. First, we will implement a rigid camera, however, using a script and not be parenting the camera under the player object.

2.2.1. Rigid follow camera

Write a script, Scripts/RigidFollowCamera.cs, that is a component for a camera object. Below are requirements and hints:

  • Give RigidFollowCamera.cs a public Transform target parameter that is set to the player

  • Give RigidFollowCamera.cs parameters to represent the desired horizontal and vertical distances from the center of the player character

A05 RigidFollowCamera

The algorithm for a basic follow camera is as follows (Madhav Chapter 8). Implement the following in LateUpdate() for a smoother result.

// tPos, tUp, tForward = Position, up, and forward vector of target
// hDist = horizontal follow distance
// vDist = vertical follow distance

// Camera position is offset from the target position
Vector3 eye = tPos - tForward * hDist + tUp * vDist

// The direction the camera should point is from the target to the camera position
Vector3 cameraForward = tPos - eye

// Set the camera's position and rotation with the new values
// This code assumes that this code runs in a script attached to the camera
transform.position = eye;
transform.rotation = Quaternion.LookRotation(cameraForward);

Some questions to think about when you implement this feature:

  • How can we get the forward and up directions of a GameObject in Unity?

  • How can we set parameters in a script so that we can give them values in the Unity IDE?

A05 RigidCamera

2.2.2. Spring follow camera

Write a script, Scripts/SpringFollowCamera.cs, that is a component for a camera object. Your Spring Follow Camera will support the same controls as the rigid camera; however, instead of rigidly following the player character, we will gently and smoothly follow the player using spring dynamics. The requirements and hints are be the same as before.

  • Give SpringFollowCamera.cs a public Transform target parameter that is set to the player

  • Give SpringFollowCamera.cs parameters to represent the

    • desired horizontal distance

    • desired vertical distance

    • the dampening factor, dampConstant

    • the spring constant, springConstant

The algorithm for a basic spring follow camera is as follows (Madhav Chapter 8). Implement this in LateUpdate() for smoother animation.

// tPos, tUp, tForward = Position, up, and forward vector of target
// hDist = horizontal follow distance
// vDist = vertical follow distance

// Camera position is offset from the target position
Vector3 idealEye = tPos - tForward * hDist + tUp * vDist

// The direction the camera should point is from the target to the camera position
Vector3 cameraForward = tPos - actualPosition

// Compute the acceleration of the spring, and then integrate
Vector3 displacement = actualPosition - idealEye
Vector3 springAccel = (-springConstant * displacement) - (dampConstant * velocity)

// Update the camera's velocity based on the spring acceleration
velocity += springAccel * deltaTime
actualPosition += velocity * deltaTime

// Set the camera's position and rotation with the new values
// This code assumes that this code runs in a script attached to the camera
transform.position = actualPosition;
transform.rotation = Quaternion.LookRotation(cameraForward);
A05 SpringCamera

3. What to hand in

  • Your scene with your level and player in it

  • Your scripts, PlayerControls.cs, RigidFollowCamera.cs, and SpringFollowCamera.cs

  • Updated README

    • A video or GIF showing the rigid camera and spring follow camera

    • Credits/links for any assets that you did not make yourself — if you add assets this week.

Don’t forget to commit your changes and then push them to Github. Using Git Bash, run the following command inside your HelloUnity directory.

$ cd HelloUnity
$ git add .
$ git commit -m "Finished HelloUnity"
$ git push

Run git status to check the result of the previous git command. Check the Github website to make sure that your program uploaded correctly.

Assignment rubrics

Grades are out of 4 points.

  • Your scripts, PlayerControls.cs, RigidFollowCamera.cs, and SpringFollowCamera.cs (3 points)

    • Player controls should have parameters: turn speed, move speed

    • Player controls should use WASD input

    • RigidFollowCamera should have parameters: target, vDist, and hDist

    • SpringFollowCamera should have parameters: target, vDist, hDist, springConstant, dmpConstant

  • Readme (1 point)

Submission rubrics

For full credit, your submission must

  • In your README, include videos or gifs showing your working scripts

  • Some credit lost for missing features or bugs, depending on severity of error

  • -100% for failure to commit work to Github

  • -100% for failure to build and run on the lab Windows machines